﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Common;

namespace Dal
{
    public class TaoBaoUser
    {
        public TaoBaoUser()
        {
            logintime = DateTime.Now;
        }

        #region Model

        private string _nick;
        private string _tokentype;
        private string _token;
        private string _refreshtoken;
        private DateTime? _expiretime;
        private DateTime _logintime = DateTime.Now;
        private string _email;

        /// <summary>
        /// 
        /// </summary>
        public long TBuid { get; set; }

        /// <summary>
        /// 旺旺账号
        /// </summary>
        public string nick
        {
            set { _nick = value; }
            get { return _nick; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string TokenType
        {
            set { _tokentype = value; }
            get { return _tokentype; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string Token
        {
            set { _token = value; }
            get { return _token; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string RefreshToken
        {
            set { _refreshtoken = value; }
            get { return _refreshtoken; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime? ExpireTime
        {
            set { _expiretime = value; }
            get { return _expiretime; }
        }
        /// <summary>
        /// 
        /// </summary>
        public DateTime logintime
        {
            set { _logintime = value; }
            get { return _logintime; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string email
        {
            set { _email = value; }
            get { return _email; }
        }

        /// <summary>
        /// 
        /// </summary>
        public byte WeiBoUserNum { get; set; }


        #endregion Model

        #region  Method

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(string ip, string browser, string os, string agent)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [TaoBaoUser] (");
            strSql.Append("TBuid,nick,TokenType,Token,RefreshToken,ExpireTime,WeiBoUserNum)");
            strSql.Append(" values (");
            strSql.Append("@TBuid,@nick,@TokenType,@Token,@RefreshToken,@ExpireTime,@WeiBoUserNum)");
            SqlParameter[] parameters = {
					new SqlParameter("@TBuid", SqlDbType.BigInt,8),
					new SqlParameter("@nick", SqlDbType.NVarChar,50),
					new SqlParameter("@TokenType", SqlDbType.NVarChar,50),
					new SqlParameter("@Token", SqlDbType.NVarChar,100),
					new SqlParameter("@RefreshToken", SqlDbType.NVarChar,100),
					new SqlParameter("@ExpireTime", SqlDbType.SmallDateTime),
					new SqlParameter("@WeiBoUserNum", SqlDbType.TinyInt,1)};
            parameters[0].Value = TBuid;
            parameters[1].Value = nick;
            parameters[2].Value = TokenType;
            parameters[3].Value = Token;
            parameters[4].Value = RefreshToken;
            parameters[5].Value = ExpireTime;
            parameters[6].Value = WeiBoUserNum;
            strSql.AppendFormat(" INSERT INTO [TaoBaoUserLog]([TBuid],[ip],[browser],[os],[agent])VALUES({0},'{1}','{2}','{3}','{4}')", TBuid, ip, browser, os, agent);
           int rows = 0;
            try
            {
                rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            }
            catch (Exception e)
            {
                if (e.Message.Contains("插入重复键"))
                {
                    rows = Update(ip, browser, os, agent);
                }
            }
            return rows;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(string ip, string browser, string os, string agent)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [TaoBaoUser] set ");
            strSql.Append("nick=@nick,");
            strSql.Append("TokenType=@TokenType,");
            strSql.Append("Token=@Token,");
            strSql.Append("RefreshToken=@RefreshToken,");
            strSql.Append("ExpireTime=@ExpireTime");
            strSql.Append(" where TBuid=@TBuid ");
            SqlParameter[] parameters = {
					new SqlParameter("@nick", SqlDbType.NVarChar,50),
					new SqlParameter("@TokenType", SqlDbType.NVarChar,50),
					new SqlParameter("@Token", SqlDbType.NVarChar,100),
					new SqlParameter("@RefreshToken", SqlDbType.NVarChar,100),
					new SqlParameter("@ExpireTime", SqlDbType.SmallDateTime),
					new SqlParameter("@TBuid", SqlDbType.BigInt,8)};
            parameters[0].Value = nick;
            parameters[1].Value = TokenType;
            parameters[2].Value = Token;
            parameters[3].Value = RefreshToken;
            parameters[4].Value = ExpireTime;
            parameters[5].Value = TBuid;
            strSql.AppendFormat(" INSERT INTO [TaoBaoUserLog]([TBuid],[ip],[browser],[os],[agent])VALUES({0},'{1}','{2}','{3}','{4}')", TBuid, ip, browser, os, agent);
           
            return  DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [TaoBaoUser] ");
            strSql.Append(" where uid=@uid ");
            SqlParameter[] parameters = {
					new SqlParameter("@uid", SqlDbType.Int,4)};
            parameters[0].Value = uid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [TaoBaoUser] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 根据uid返回nike
        /// </summary>
        /// <param name="uid">用户id</param>
        /// <returns></returns>
        public string GetNikeByUid(string uid)
        {
            string sql = string.Format("select nick from TaoBaoUser where uid={0}", uid);
            object obj = DbHelperSQL.GetSingle(sql);
            return obj == null ? "" : obj.ToString();
        }
        #endregion  Method
    }
}

